package com.ip.getip.util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description TODO
 * @Author leixin
 * @Date 2019/9/28 下午2:34
 * @Version 1.0
 **/
@Component
public class FileImg {
    //获取图片用
    public String downloadPicture(String urlList, String path) {
        URL url = null;
        DataInputStream dataInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            url = new URL(urlList);
            dataInputStream = new DataInputStream(url.openStream());
            fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //始终进行关闭流的操作
            try {
                dataInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    //创建文件夹
    public static String mkdir(String path){
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return file.getPath();
    }

    //获取图片地址
    public static void getImg(String url, String path,String cookie) throws Exception {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        connection.setRequestProperty("Cookie", cookie);
        // 建立实际的连接
        connection.connect();
        // 获取所有响应头字段
        Map<String, List<String>> map = connection.getHeaderFields();
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(
                connection.getInputStream(), "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        String[] split = result.toString().split("Large_cgurl = new Array\\(\\)\\;");
        String[] split2 = split[1].toString().split("i=0\\;");
        String trim = split2[0].trim();
        List list = new ArrayList();
        String[] text = trim.split("\\;");
        for (String s : text) {
            list.add(s.split("\"")[1]);
        }
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new Runnable() {
            @Override
            public void run() {
                list.forEach(e->{
                    FileImg fileImg = new FileImg();
                    String paths = e.toString().substring(e.toString().length() - 7, e.toString().length());
                    //下载图片
                    fileImg.downloadPicture(e.toString(), path+paths);
                });
            }
        });
    }
}
